import { parse } from './parser/index'
import { optimize } from './optimizer'
import { generate } from './codegen/index'
import { createCompilerCreator } from './create-compiler'
import { CompilerOptions, CompiledResult } from 'types/compiler'

// `createCompilerCreator` allows creating compilers that use alternative
// parser/optimizer/codegen, e.g the SSR optimizing compiler.
// Here we just export a default compiler using the default parts.
export const createCompiler = createCompilerCreator(function baseCompile(
  template: string,
  options: CompilerOptions
): CompiledResult {

  // 1. 解析模板生成抽象语法树对象(利用大量正则)
  const ast = parse(template.trim(), options)

  // 2. 对语法树进行优化(添加静态标识属性, 用于后面虚拟DOM Diff处理优化)
    // 静态节点对应的vnode在后面进行 vdom diff处理可以不用再次生成新的vnode 和真实DOM
    // static标识属性: 如果为true就是静态节点
  if (options.optimize !== false) {
    optimize(ast, options)
  }

  // 3. 根据ast生成用于生成虚拟DOM的render函数(准确的说是表达式代码)
  const code = generate(ast, options)

  return {
    ast,
    render: code.render,
    staticRenderFns: code.staticRenderFns
  }
})
